package com.csdj.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csdj.server.pojo.Menu;
import com.csdj.server.pojo.MenuRole;
import com.csdj.server.pojo.RespBean;
import com.csdj.server.pojo.Role;
import com.csdj.server.service.IMenuRoleService;
import com.csdj.server.service.IMenuService;
import com.csdj.server.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: PermissController
 * @Author: ZouLi
 * @Description:
 * @Date: 2022/12/28 9:05
 **/
@RestController
@RequestMapping("/system/basic/permiss")
@Api(tags = "权限组控制器PermissController")
public class PermissController {
    /**
     * 注入 roleService
     **/
    @Autowired
    private IRoleService roleServiceImpl;

    /**
     * 注入 menuService
     **/
    @Autowired
    private IMenuService menuServiceImpl;

    /**
     * 注入 menuRoleService
     **/
    @Autowired
    private IMenuRoleService menuRoleServiceImpl;

    /**
     * @Description: 获取全部角色信息
     * @Param []
     * @Return java.util.List<com.csdj.server.pojo.Role>
     * @Throws
     **/
    @ApiOperation(value = "获取全部角色信息")
    @GetMapping("/getRoles")
    public List<Role> getRoles(){
        return roleServiceImpl.list();
    }

    /**
     * @Description: 添加角色信息
     * @Param [role]
     * @Return com.csdj.server.pojo.RespBean
     * @Throws
     **/
    @ApiOperation(value = "添加角色信息")
    @PostMapping("/addRole")
    public RespBean addRole(@RequestBody Role role){
        if (!role.getName().startsWith("ROLE_")) {
            role.setName("ROLE_"+role.getName());
        }
        if (roleServiceImpl.save(role)) {
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    /**
     * @Description: 删除角色信息
     * @Param [id]
     * @Return com.csdj.server.pojo.RespBean
     * @Throws
     **/
    @ApiOperation(value = "删除角色信息")
    @DeleteMapping("/deleteRole/{rid}")
    public RespBean deleteRole(@PathVariable Integer rid){
        if (roleServiceImpl.removeById(rid)) {
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }

    /**
     * @Description: 获取全部菜单
     * @Param []
     * @Return java.util.List<com.csdj.server.pojo.Menu>
     * @Throws
     **/
    @ApiOperation(value = "获取全部菜单")
    @GetMapping("/getAllMenus")
    public List<Menu> getAllMenus(){
        return menuServiceImpl.getAllMenus();
    }

    /**
     * @Description: 更新菜单角色
     * @Param [rid, mids]
     * @Return com.csdj.server.pojo.RespBean
     * @Throws
     **/
    @ApiOperation(value = "更新菜单角色")
    @PutMapping("/updateMenuRole")
    public RespBean updateMenuRole(Integer rid, Integer[] mids){
        return menuRoleServiceImpl.updateMenuRole(rid, mids);
    }

    /**
     * @Description: 根据角色查询菜单信息
     * @Param [rid]
     * @Return java.util.List<java.lang.Integer>
     * @Throws
     **/
    @ApiOperation(value = "根据角色查询菜单信息")
    @GetMapping("/getMenuByRole/{rid}")
    public List<Integer> getMenuByRole(@PathVariable Integer rid){
        List<MenuRole> menuRoles = menuRoleServiceImpl.list(new QueryWrapper<MenuRole>().eq("rid", rid));
        return menuRoles.stream().map(MenuRole::getMid).collect(Collectors.toList());
    }
}
